package top.xunfong.knowledge.service.tencent;

import com.tencentcloudapi.common.AbstractModel;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.lke.v20231130.LkeClient;
import com.tencentcloudapi.lke.v20231130.models.CreateSharedKnowledgeRequest;
import com.tencentcloudapi.lke.v20231130.models.CreateSharedKnowledgeResponse;
import com.tencentcloudapi.lke.v20231130.models.ListDocRequest;
import com.tencentcloudapi.lke.v20231130.models.ListDocResponse;
import jakarta.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * @author: xunfong
 * @date: 2025/10/26 22:36
 */
@Component
public class TencentApiService {

    @Value("${tencent.api.SecretId:}")
    private String SECRET_ID = "AKIDdDg1CE7vWu55rS9lAyZzCC1e6KK8lDST";

    @Value("${tencent.api.SecretKey:}")
    private String SECRET_KEY = "rXlmbGhvwkNm166tVxCCzb1FIBrKbgaH";

    @Value("${tencent.api.Region:ap-guangzhou}")
    private String REGION = "ap-guangzhou";

//    private Credential cred;

    private LkeClient client;


    @PostConstruct
    private void init() {
        client = new LkeClient(new Credential(SECRET_ID, SECRET_KEY), REGION);
    }


    /**
     * 获取知识库列表
     */
    public void getKnowledgeList() throws TencentCloudSDKException {
//        Credential cred = new Credential(SECRET_ID, SECRET_KEY);
        // 使用临时密钥示例
        // Credential cred = new Credential("SecretId", "SecretKey", "Token");
        // 实例化一个http选项，可选的，没有特殊需求可以跳过
//        HttpProfile httpProfile = new HttpProfile();
//        httpProfile.setEndpoint("lke.tencentcloudapi.com");
        // 实例化一个client选项，可选的，没有特殊需求可以跳过
//        ClientProfile clientProfile = new ClientProfile();
//        clientProfile.setHttpProfile(httpProfile);
        // 实例化要请求产品的client对象,clientProfile是可选的
//        LkeClient client = new LkeClient(cred, "ap-guangzhou");
        // 实例化一个请求对象,每个接口都会对应一个request对象
        ListDocRequest req = new ListDocRequest();
        req.setBotBizId("1981678207473102976");
        req.setPageNumber(1L);
        req.setPageSize(10L);

        // 返回的resp是一个ListDocResponse的实例，与请求对象对应
        ListDocResponse resp = client.ListDoc(req);
        // 输出json格式的字符串回包
        System.out.println(AbstractModel.toJsonString(resp));
    }

    /**
     * 创建共享知识库
     *
     * @param name
     * @param description
     */
    public CreateSharedKnowledgeResponse createSharedKnowledge(String name, String description) throws TencentCloudSDKException {

        // 实例化一个请求对象,每个接口都会对应一个request对象
        CreateSharedKnowledgeRequest req = new CreateSharedKnowledgeRequest();
        req.setKnowledgeName(name);
        req.setKnowledgeDescription(description);
        // 返回的resp是一个CreateSharedKnowledgeResponse的实例，与请求对象对应
        return client.CreateSharedKnowledge(req);
    }

    public static void main(String[] args) throws TencentCloudSDKException {
        TencentApiService tencentApiService = new TencentApiService();
        tencentApiService.init();
        tencentApiService.createSharedKnowledge("测试创建1", "描述1");
    }
}
